<template>
  <div v-if="orderList.length > 0" class="order">
    <div class="order-item" v-for="item in orderList" :key="item.id">
      <div class="order-item--img-contain">
        <img
          class="order-item--img"
          :src="`${imgPrefix}${item.thumbnailUrl}`"
        />
      </div>
      <div class="order-item--content">
        <div class="order-item--title">
          {{ item.productName }}
        </div>
        <div class="order-item--subtitle">
          兑换时间：{{ formateDate(item.exchangeTime) }}
        </div>
        <div v-if="item.status === 1" class="order-item--subtitle">待付款</div>
        <div v-else-if="item.status === 2" class="order-item--subtitle">
          有效时间：{{ formateDate(item.expireTime) }}
        </div>
        <div v-else-if="item.status === 3" class="order-item--subtitle">
          {{ item.productType === 2 ? '充值中' : '提取中' }}
        </div>
        <div v-else-if="item.status === 4" class="order-item--subtitle">
          使用时间：{{ formateDate(item.useTime) }}
        </div>
        <div v-else-if="item.status === 5" class="order-item--subtitle">
          {{ item.productType === 2 ? '充值中' : '提取中' }}
        </div>
        <div v-else-if="item.status === 6" class="order-item--subtitle">
          {{ item.productType === 2 ? '充值中' : '提取中' }}
        </div>
        <div v-else-if="item.status === 7" class="order-item--subtitle">
          已取消
        </div>
      </div>
      <div class="order-item--btn" @click="onClickOrder(item)">
        {{ getItemButtonText(item) }}
      </div>
    </div>
  </div>
  <div v-else-if="!isLoading" class="order-empty">
    <img class="order-empty__img" src="../../assets/order-empty.png" />
    <div class="order-empty__text">还没有兑换，快去兑换吧～</div>
  </div>
  <div v-else-if="isLoading" class="load-page">
    <van-loading type="spinner" />
  </div>
</template>

<script>
import { reactive, onMounted, toRef } from 'vue'
import { useRouter } from 'vue-router'
import { Toast } from 'vant'
import { formateDate } from '@/utils'
import globalVariables from '@/utils/globalVariables'
import api from '@/api'

export default {
  setup() {
    const router = useRouter()

    // const order1 = {
    //   id: 1,
    //   productName: '京东PLUS会员季卡',
    //   orderCode: 'JD1239862848',
    //   exchangeTime: '2021年11月25日',
    //   expireTime: '2021年01月25日 09:40:52',
    //   useTime: '2021年11月25日',
    //   status: 0,
    //   thumbnailUrl: require('../../assets/product-JD.png'),
    //   productType: 2
    // }
    // const order2 = {
    //   id: 2,
    //   productName: '网易云音乐黑胶VIP季卡',
    //   orderCode: 'JD1239862848',
    //   exchangeTime: '2021年11月25日',
    //   expireTime: '2021年01月25日 09:40:52',
    //   useTime: '2021年11月25日',
    //   status: 2,
    //   thumbnailUrl: require('../../assets/product-JD.png'),
    //   productType: 2
    // }
    // const data = reactive({
    //   orderList: [order1, order2]
    // })

    const data = reactive({
      orderList: [],
      isLoading: true
    })

    const onClickOrder = order => {
      console.log('orderCode', order.orderCode)
      router.push({
        path: '/orderDetails',
        query: { orderCode: order.orderCode }
      })
    }

    const getOrderList = async () => {
      data.isLoading = true
      const res = await api.getOrderList()
      data.isLoading = false
      if (res.error) {
        Toast(res.error.message)
        return
      }
      data.orderList = res.data
    }

    const getItemButtonText = item => {
      if (item.status === 2) {
        return '去使用 >'
      }
      return '去查看 >'
    }

    onMounted(() => {
      document.title = '权益记录'
      getOrderList()
    })

    return {
      orderList: toRef(data, 'orderList'),
      isLoading: toRef(data, 'isLoading'),
      imgPrefix: globalVariables.imgPrefix,
      formateDate,
      onClickOrder,
      getItemButtonText
    }
  }
}
</script>

<style lang="less" scoped="scoped">
.order {
  background-color: #ececec;
  min-height: 100vh;
  overflow: hidden;
  padding-bottom: 18px;
  .order-item {
    width: 375px;
    height: 80px;
    background: #ffffff;
    box-shadow: 0px 1px 0px 0px #999999;
    border-radius: 1px;
    margin-top: 10px;

    display: flex;
    align-items: center;
    .order-item--img-contain {
      min-width: 52px;
      min-height: 52px;
      width: 52px;
      height: 52px;
      background-image: url(../../assets/order-radius.png);
      background-size: 100% 100%;
      box-sizing: content-box;
      margin-left: 17px;

      .order-item--img {
        margin: 3px 0 0 3px;
        width: 44px;
        height: 44px;
      }
    }
    .order-item--content {
      margin-left: 7px;
      flex: 1;
      .order-item--title {
        font-size: 16px;
        font-family: Source Han Sans CN;
        font-weight: 500;
        color: #333333;
      }
      .order-item--subtitle {
        font-size: 11px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #333333;
      }
    }
    .order-item--btn {
      width: 69px;
      height: 25px;
      margin-right: 21px;

      border-radius: 18px;
      background: linear-gradient(-4deg, #ebd9c0, #efb26d);
      box-shadow: 0px 1px 0px 0px rgba(0, 0, 0, 0.67),
        0px 1px 0px 0px rgba(255, 255, 255, 0.96);

      font-size: 12px;
      font-family: Source Han Sans CN;
      font-weight: 500;
      color: #333333;

      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}
.order-empty {
  background-color: #ffffff;
  min-height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  .order-empty__img {
    margin-top: -80px;
    width: 300px;
    height: 190px;
  }
  .order-empty__text {
    margin-top: 27px;
    font-size: 14px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #999999;
  }
}
</style>
